/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package wat.serwerapp.main;

import java.io.*;
import java.net.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import wat.serwerapp.connection.Receiver;
import wat.ziszcommon.transport.*;

/**
 *
 * @author Sebateee
 */
public class ObslugaKlienta extends Thread {
/*
   private Socket gniazdo;

   
   class UnknownMessageException extends IOException {
   }

   public ObslugaKlienta(Socket sock) throws IOException {
      gniazdo = sock;
      start();
   }

   private Message Input() {
      try {
         // System.out.println(gniazdo.toString());
         ObjectInputStream wejscie = new ObjectInputStream(gniazdo.getInputStream());
         Message obj = (Message) wejscie.readObject();
         return obj;
      } catch (IOException e) {
         Logger.getLogger(ObslugaKlienta.class.getName()).log(Level.SEVERE, "serwer: nieudany odbior", e);
         return null;
      } catch (ClassNotFoundException e) {
         Logger.getLogger(ObslugaKlienta.class.getName()).log(Level.SEVERE, "serwer: odbior nieznanej klasy", e);
         return null;
      }
   }

   private boolean Output(Object msg) {
      try {
         ObjectOutputStream wyjscie = new ObjectOutputStream(gniazdo.getOutputStream());
         wyjscie.writeObject(msg);
         wyjscie.flush();
         wyjscie.close();
         return true;
      } catch (IOException ex) {
         Logger.getLogger(ObslugaKlienta.class.getName()).log(Level.SEVERE, "serwer: nieudane wyslanie odpowiedzi", ex);
         return false;
      }
   }

   private Object Process(Message m) throws UnknownMessageException {
      String sessionID = m.getSessionID();
      if (m.getClass().equals(MessagePKI.class)) {
         MessagePKI mPKI = (MessagePKI) m;
         return Receiver.preparePKI(sessionID, mPKI.getP(), mPKI.getG(), mPKI.getL(), mPKI.getPublicKey());
      } else if (m.getClass().equals(MessageUsrSession.class)) {
         MessageUsrSession mUserAction = (MessageUsrSession) m;
         MessageUsrSession.Action act = mUserAction.getAction();
         switch (act) {
            case LOGIN :
               return Receiver.login(sessionID, mUserAction.getUserName(), mUserAction.getPassword());
            case KEEP_ALIVE :
               return Receiver.keepAlive(sessionID);
            case LOGOUT :
               return Receiver.logout(sessionID);
         }
         return "not OK";
      } else if (m.getClass().equals(MessageMethod.class)) {
         MessageMethod mMethod = (MessageMethod) m;
         return Receiver.invokeMethod(sessionID, mMethod.getMethodName(), mMethod.getParameters(), mMethod.getModule(),
                 mMethod.getAdv());
      } else if(m.getClass().equals(MethodCallMessage.class)) {
          return AccessControler.getInstance().processMethodCall((MethodCallMessage)m);
      } else {
         throw new UnknownMessageException();
         //return null;
      }

   }

   @Override
   public void run() {
      long ident = getId();
      System.out.println("- Watek obslugi [" + ident + "]");
      try {
         Message dane = Input();           //przyjęcie danych od klienta
         Object response = Process(dane);  //przetworzenie
         Output(response);                 //wysyłanie danych
      } catch (UnknownMessageException e) {
         Output("Unrecognized message type");
      } catch (Exception e) {
      } finally {
         try {
            System.out.println("[" + ident + "] Rozlaczanie klienta - " + gniazdo.getRemoteSocketAddress());
            gniazdo.close();
         } catch (IOException e) {
         }
      }
   }*/
}
